//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package kd.ec.basedata.common.update;

import java.util.Iterator;
import java.util.Set;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.dataentity.metadata.clr.DataEntityPropertyCollection;
import kd.bos.dataentity.metadata.dynamicobject.DynamicLocaleProperty;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.property.*;
import kd.ec.basedata.common.utils.BillUpdateHelper;

public abstract class BillUpdateCommonPlugin extends AbstractBillPlugIn {
    protected BillUpdateHelper updateHelper = new BillUpdateHelper();

    public BillUpdateCommonPlugin() {
    }

    public void initIgnoreFields(BillUpdateHelper newHelper) {
        this.updateHelper = newHelper;
    }

    protected void initSourceData(DynamicObject source) {
        MainEntityType dataEntityType = EntityMetadataCache.getDataEntityType(this.getView().getFormShowParameter().getFormId());
        DataEntityPropertyCollection properties = dataEntityType.getProperties();
        DynamicObjectType projectType = source.getDynamicObjectType();
        Set<Object> ignoreFields = this.updateHelper.ignoreFields;
        Iterator var6 = properties.iterator();

        while(var6.hasNext()) {
            IDataEntityProperty property = (IDataEntityProperty)var6.next();
            if (property instanceof EntryProp) {
                this.handleEntry(property, source);
            } else if (!this.handleSomeFields(property, source)) {
                String name = property.getName();
                if (!name.endsWith("_id") && !ignoreFields.contains(name) && projectType.getProperty(name) != null) {
                    this.getModel().setValue(name, source.get(name));
                }
            }
        }

        this.getModel().updateCache();
    }

    protected boolean handleSomeFields(IDataEntityProperty property, DynamicObject source) {
        boolean flag = false;
        if (property instanceof DynamicLocaleProperty) {
            flag = true;
        } else if (property instanceof MulBasedataProp) {
            DynamicObjectCollection sourceMultiData = source.getDynamicObjectCollection(property.getName());
            DynamicObjectCollection multiData = (DynamicObjectCollection)this.getModel().getValue(property.getName());
            multiData.addAll(sourceMultiData);
            this.getModel().setValue(property.getName(), multiData);
            flag = true;
        } else if (property instanceof ItemClassProp) {
            ItemClassProp itemClassProp = (ItemClassProp)property;
            String typeName = itemClassProp.getTypePropName();
            this.getModel().setValue(typeName, source.get(typeName));
            this.getModel().setValue(property.getName(), source.get(property.getName()));
            flag = true;
        } else if (property instanceof ItemClassTypeProp) {
            flag = true;
        }

        return flag;
    }

    protected void handleEntry(IDataEntityProperty property, DynamicObject source) {
        String formId = this.getView().getFormShowParameter().getFormId();
        EntryProp entryProp = (EntryProp)property;
        DynamicObjectType entryType = entryProp.getDynamicCollectionItemPropertyType();
        Set<String> isUpdateSet = this.getIsUpdateSet();
        Set<Object> ignoreFields = this.updateHelper.ignoreFields;
        if (entryType != null) {
            DataEntityPropertyCollection entryProps = entryType.getProperties();
            DynamicObjectCollection billEntry = this.getModel().getEntryEntity(property.getName());
            DynamicObjectCollection sourceEntry = source.getDynamicObjectCollection(property.getName());
            DynamicObjectType sourceType = sourceEntry.getDynamicObjectType();
            Iterator var11 = sourceEntry.iterator();

            while(var11.hasNext()) {
                DynamicObject entity = (DynamicObject)var11.next();
                DynamicObject entry = billEntry.addNew();
                entry=handleEntryDefault(property.getName(),entry,entity);
                Iterator var14 = entryProps.iterator();
                while(var14.hasNext()) {
                    IDataEntityProperty prop = (IDataEntityProperty)var14.next();
                    String name = prop.getName();
                    if (isUpdateSet.contains(name)) {
                        entry.set(prop.getName(), false);
                    } else if (!ignoreFields.contains(name) && sourceType.getProperty(name) != null) {
                        if("ecbd_projectupdate".equalsIgnoreCase(formId)){
                            if (prop instanceof SubEntryProp) {//子单据体
                                DynamicObjectCollection subEntity=(DynamicObjectCollection) entity.get(prop.getName());
                                handleSubEntry(prop,entry,subEntity);
                            }else{
                                entry.set(prop.getName(), entity.get(prop.getName()));
                            }
                        }else{
                            entry.set(prop.getName(), entity.get(prop.getName()));
                        }
                    }

                }
            }

            this.getModel().updateEntryCache(billEntry);
            this.getView().updateView(property.getName());
        }

    }

    public DynamicObject handleEntryDefault(String entityKey,  DynamicObject entryNewRow,DynamicObject entitySourceRow){
        String formId = this.getView().getFormShowParameter().getFormId();
        switch (formId){
            case "ecbd_projectupdate":
                if("hc95_building".equalsIgnoreCase(entityKey)){//楼栋
                    String hc95_sourceid_ld = entitySourceRow.getString("id");
                    entryNewRow.set("hc95_sourceid_ld",hc95_sourceid_ld);
                }else if("hc95_building_ds".equalsIgnoreCase(entityKey)){//楼栋指标(地上)
                    String hc95_sourceid_ds = entitySourceRow.getString("id");
                    entryNewRow.set("hc95_sourceid_ds",hc95_sourceid_ds);
                }else if("hc95_building_dx".equalsIgnoreCase(entityKey)){//楼栋指标(地下)
                    String hc95_sourceid_dx = entitySourceRow.getString("id");
                    entryNewRow.set("hc95_sourceid_dx",hc95_sourceid_dx);
                }
                break;
        }
        return entryNewRow;
    }

    protected void handleSubEntry(IDataEntityProperty property,  DynamicObject entryParent,DynamicObjectCollection sourceSubEntry) {
        SubEntryProp entrySubProp = (SubEntryProp)property;
        DynamicObjectType entrSubType = entrySubProp.getDynamicCollectionItemPropertyType();
        Set<String> isUpdateSet = this.getIsUpdateSet();
        Set<Object> ignoreFields = this.updateHelper.ignoreFields;
        if (entrSubType != null) {
            DataEntityPropertyCollection entrySubProps = entrSubType.getProperties();
            DynamicObjectCollection billSubEntry = entryParent.getDynamicObjectCollection(property.getName());
            DynamicObjectType sourceType = sourceSubEntry.getDynamicObjectType();
            Iterator var11 = sourceSubEntry.iterator();

            while(var11.hasNext()) {
                DynamicObject entity = (DynamicObject)var11.next();
                DynamicObject entry = billSubEntry.addNew();
                entry=handleEntryDefault(entrySubProp.getName(),entry,entity);
                Iterator var14 = entrySubProps.iterator();

                while(var14.hasNext()) {
                    IDataEntityProperty prop = (IDataEntityProperty)var14.next();
                    String name = prop.getName();
                    if (isUpdateSet.contains(name)) {
                        entry.set(prop.getName(), false);
                    } else if (!ignoreFields.contains(name) && sourceType.getProperty(name) != null) {
                        entry.set(prop.getName(), entity.get(prop.getName()));
                    }
                }
            }

            this.getModel().updateEntryCache(billSubEntry);
            this.getView().updateView(property.getName());
        }

    }



    protected abstract Set<String> getIsUpdateSet();
}
